//
//  BaseModel.swift
//  contactDemo_swift
//
//  Created by 郭颢源 on 2020/8/7.
//  Copyright © 2020 test. All rights reserved.
//

import UIKit
@objcMembers
class BaseModel: NSObject {
//    var name: String?
//
//    var age: Int = 0
//
//    var title: String?

    

    class func propertyList() -> [String] {

        var arr: [String] = []

        var count: UInt32 = 0

        let list = class_copyPropertyList(self, &count)

        print("参数的长度为\(count)")

        for i in 0..<Int(count) {

            let pty = list?[i]

            //得到C字符串

            let cName = property_getName(pty!)

            //将C字符串转化为String

            let name = String(utf8String: cName)

            arr.append(name!)

        }

        return arr

    }

    
}
extension NSObject{

    /// 获取类的属性列表
    ///
    /// - Returns:  属性名列表
    func get_class_copyPropertyList()->[String]{
        var outCount:UInt32 = 0
        let propers:UnsafeMutablePointer<objc_property_t>! =  class_copyPropertyList(self.classForCoder, &outCount)
        let count:Int = Int(outCount);
        if count==0 {
            return [""]
        }
        var names:[String] = [String]()
        for i in 0...(count-1) {
            let aPro: objc_property_t = propers[i]
            if let proName:String = String(utf8String: property_getName(aPro)){
                names.append(proName)
            }
        }
        return names
    }
    
    /// 获取类的方法列表
    ///
    /// - Returns: 方法名列表
    func get_class_copyMethodList() -> [String]{
        var outCount:UInt32
        outCount = 0
        let methods:UnsafeMutablePointer<objc_property_t>! =  class_copyMethodList(self.classForCoder, &outCount)
        let count:Int = Int(outCount);
        var names:[String] = [String]()
        for i in 0...(count-1) {
            
            let aMet: objc_property_t = methods[i]
            
            if let methodName:String = String(utf8String: property_getName(aMet)){
                names.append(methodName)
            }
        }
        return names
    }
}
